<?php
/**
 * User: zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/11/20
 * Time: 03:21
 */

namespace App\Http\Controllers\V1;

use Laravel\Lumen\Routing\Controller;
use App\Http\Services\MarkService;
use App\Http\Services\UserService;
use App\Http\Services\WorkService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use App\Providers\MyJwt;

class TeacherController extends UserController
{

    public function __construct(Request $request)
    {
        parent::__construct($request);
    }

    public function workList(Request $request)
    {
        $this->sendGet(function() use($request) {
            $page = $request->get('page');
            $pageSize = $request->get('page_size');
            $offset = ($page - 1) * $pageSize;
            $order = $request->get('order');
            $categoryId = $request->get('category_id');
            $option = [];
            if ($categoryId) {
                $option['category_id'] = $categoryId;
            }
            $data = WorkService::getList($pageSize, $offset, $order, $option);
            $this->sendJson($data);
        });
    }

    public function getDetail(Request $request)
    {
        $this->sendGet(function() use($request) {
            $id = $request->get('id');
            try {
                $data = WorkService::getDetail($id);
                $uid = $this->jwtCode['uid'];
                $localScore = [];
                foreach ($data['score_list'] as $score) {
                    if ($score['teacher']['id'] == $uid) {
                        $localScore = [
                            'teacher_id' => $uid,
                            'score' => $score['score']
                        ];
                    }
                }
                if (!empty($localScore)) {
                    $data['local_score'] = $localScore;
                }
                $this->sendJson($data);
            } catch (\Exception $e) {
                $this->sendJson([], $e->getCode(), $e->getMessage());
            }
        });
    }

    public function saveScore(Request $request)
    {
        $this->sendPost(function() use($request) {
            $params = $request->get('params');
            $data = [
                'work_id' => $params['id'],
                'teacher_id' => $this->jwtCode['uid'],
                'score' => $params['local_score']
            ];
            try {
                MarkService::save($data);
                $marks = MarkService::getListByWorkId($data['work_id']);
                $scores = 0.00;
                foreach ($marks as $mark) {
                    $scores += $mark['score'];
                }
                $score = round($scores / count($marks), 2);
                WorkService::save(['id' => $data['work_id'], 'score' => $score]);
                $this->sendJson([]);
            } catch (\Exception $e) {
                $this->sendJson([], $e->getCode(), $e->getMessage());
            }
        });
    }

    public function getFile(Request $request)
    {
        $this->sendGet(function() use($request) {
            $user = $this->getUser();
            $this->sendJson($user);
        });
    }

    public function updateFile(Request $request)
    {
        $this->sendPost(function() use($request) {
            $params = $request->get('params');
            $params['id'] = $this->jwtCode['uid'];
            try {
                UserService::save($params);
                $this->sendJson([]);
            } catch (\Exception $e) {
                $this->sendJson([], $e->getCode(), $e->getMessage());
            }
        });
    }

    public function changePass(Request $request)
    {
        $this->sendPost(function() use($request) {
            $params = $request->get('params');
            $data = [
                'id' => $this->jwtCode['uid'],
                'old_pass' => md5($params['old_pass']),
                'new_pass' => md5($params['new_pass']),
                'new_pass_sec' => md5($params['new_pass_sec'])
            ];
            try {
                UserService::changePass($data);
                $this->sendJson([]);
            } catch (\Exception $e) {
                $this->sendJson([], $e->getCode(), $e->getMessage());
            }
        });
    }
}